package com.lzc.tlmail.tlmailproduct.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.lzc.tlmail.tlmailproduct.entity.ProductAttrValueEntity;
import com.lzc.tlmail.tlmailproduct.service.AttrService;
import com.lzc.tlmail.tlmailproduct.service.ProductAttrValueService;
import com.lzc.tlmail.tlmailproduct.vo.AttrrespVo;
import com.lzc.tlmail.tlmailproduct.vo.Attrvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import com.lzc.common.utils.PageUtils;
import com.lzc.common.utils.R;



/**
 * 商品属性
 *
 * @author lzc
 * @email 1029669636@gmail.com
 * @date 2020-07-25 21:57:44
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    @GetMapping("base/listforspu/{spuId}")
    public R listforspu(@PathVariable("spuId")Long spuId){
       List<ProductAttrValueEntity> productAttrValueEntities=productAttrValueService.getAttrBySpuId(spuId);
       return R.ok().put("data",productAttrValueEntities);
    }

    /**
     * 根据分类id根据不同类型得到属性规格参数
     * @param params
     * @param catelogId
     * @param type
     * @return
     */
    @GetMapping("/{type}/list/{catelogId}")
    public R baseList(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId") Long catelogId,
                      @PathVariable("type") String type){
        PageUtils pageUtils = attrService.queryBasePage(params, catelogId,type);
        if(pageUtils!=null){
            return R.ok().put("page",pageUtils);
        }else {
            return R.error();
        }

    }




    /**
     * 得到属性详情
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		//AttrEntity attr = attrService.getById(attrId);
        AttrrespVo attrrespVo= attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrrespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody Attrvo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody Attrvo attr){
		//attrService.updateById(attr);
        attrService.updateAttr(attr);
        return R.ok();
    }

    /**
     * 修改spu规格参数
     */
    @PostMapping("/update/{spuId}")
    //@RequiresPermissions("product:attr:update")
    public R update(@PathVariable("spuId") Long spuId,
                    @RequestBody List<ProductAttrValueEntity> productAttrValueEntities){
        productAttrValueService.updateSpuAttr(spuId,productAttrValueEntities);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
